package com.carrentalssm.controller;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.carrentalssm.entity.AdminUser;
import com.carrentalssm.entity.CarList;
import com.carrentalssm.service.CarListService;

/**
 *
 */
@Controller
@RequestMapping("/carList")
public class CarListController {
	
	@Autowired
	private CarListService carListService;
	
	/**
	 * 添加汽车
	 * @param carList
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(CarList carList, HttpServletRequest request) {
		carList.setId(new SimpleDateFormat("yyyMMddHHmmssSSS").format(new Date()));
		carList.setCreateDatetime(new Timestamp(new Date().getTime()));
		carList.setCreateUsername(((AdminUser) request.getSession().getAttribute("user")).getUsername());
		carListService.add(carList);
		return "redirect:/admin.jsp";
	}
	
	/**
	 * 更新汽车
	 * @param carList
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(CarList carList) {
		carListService.update(carList);
		return "redirect:/admin.jsp";
	}
	
	/**
	 * 删除汽车
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	public String delete(String id) {
		carListService.delete(id);
		return "redirect:/admin.jsp";
	}

}
